#!/usr/bin/env python2.5
# -*- coding: UTF-8 -*-  
from google.appengine.ext import db


class city  (db.Model):  
    Name =   db.StringProperty   (indexed=False,  required=True,  multiline=False, verbose_name="City name") 
    GeoPt =  db.GeoPtProperty    (indexed=False,  required=True,  verbose_name="City Geolocation") 
    TD =     db.FloatProperty    (indexed=False,  required=True,  verbose_name="City Time Difference from GMT")
    DLST =   db.FloatProperty    (indexed=False,  required=False, verbose_name="City Daylight Saving Time")
    Flg =    db.IntegerProperty  (indexed=False,  required=True,  verbose_name="Flags 2=capital")
    def json(self):
        return {"Name":self.Name, "GeoPt":[self.GeoPt.lat, self.GeoPt.lon], "TD":self.TD, "DLST":"null" if self.DLST is None else self.DLST , "Flg":self.Flg}
class cntrAux(db.Model):
    CC =            db.StringProperty   (indexed=False,  required=True,  multiline=False, verbose_name="iso 2 char country code")
    Flag160X80 =    db.BlobProperty     (required=True,  verbose_name="Flag png")
    @classmethod       
    def getByKey (self, cc): 
        return self.get_by_key_name(cc, rpc= db.create_rpc(read_policy=db.EVENTUAL_CONSISTENCY)) 
    def json(self):
        return {"CC":self.CC, "Flag160X80":self.Flag160X80 }
class cntr  (db.Model):
    Name =          db.StringProperty   (indexed=False,  required=True,  multiline=False, verbose_name="Country Name")
    CC =            db.StringProperty   (indexed=False,  required=True,  multiline=False, verbose_name="iso 2 char country code")
    CCisoN =        db.IntegerProperty  (indexed=False,  required=True,  verbose_name="iso Numeric country code")
    CCiso3 =        db.StringProperty   (indexed=False,  required=True,  multiline=False, verbose_name="iso 3 char country code")
    CCfips =        db.StringProperty   (indexed=False,  required=True,  multiline=False, verbose_name="FIPS char country code")
    CCtld =         db.StringProperty   (indexed=False,  required=True,  multiline=False, verbose_name="Internet TLD country code") 
    #CCTel =         db.StringProperty   (indexed=False,  required=False, multiline=False, verbose_name="Itnernational Tel Prefix")
    CCtel =         db.StringProperty   (indexed=False,  required=False, multiline=False, verbose_name="Itnernational Tel Prefix")
    GeoPt =         db.GeoPtProperty    (indexed=False,  required=True,  verbose_name="Country Center Location Point") 
    GeoLat =        db.FloatProperty    (indexed=False,  required=True,  verbose_name="Center Geolocation Latitude")
    GeoLon =        db.FloatProperty    (indexed=False,  required=True,  verbose_name="Center Geolocation Longitude")
    RectW =         db.FloatProperty    (indexed=False,  required=True,  verbose_name="Bounding Rectangle West")
    RectN =         db.FloatProperty    (indexed=False,  required=True,  verbose_name="Bounding Rectangle North")
    RectE =         db.FloatProperty    (indexed=False,  required=True,  verbose_name="Bounding Rectangle East")
    RectS =         db.FloatProperty    (indexed=False,  required=True,  verbose_name="Bounding Rectangle South")
    Capital=        db.ReferenceProperty(city,           required=False)  
    SeqID=          db.IntegerProperty  (indexed=False,  required=False,  verbose_name="sequence Id for this country")
    Aux =           db.ReferenceProperty(cntrAux,        required=True,  verbose_name="Flag png") 
     
    def json(self, inclCapital=False):
        rt= {"Name":self.Name, "SeqID":self.SeqID,  "CountryCodes": {"iso2":self.CC,  "isoN":self.CCisoN, "iso3":self.CCiso3, "fips":self.CCfips, "tld":self.CCtld},  \
            "TelPref":self.CCtel,"GeoPt":[self.GeoPt.lat, self.GeoPt.lon], "GeoRectangle":{"West":self.RectW, "North":self.RectN, "East":self.RectE, "South":self.RectS}}
        if inclCapital: rt["Capital"]= None if self.Capital is None else self.Capital.json()  
        return rt
    @classmethod       
    def getByKey (cls, cc): 
        return cls.get_by_key_name(cc, rpc= db.create_rpc(read_policy=db.EVENTUAL_CONSISTENCY))        
    @classmethod
    def getAll(cls):
        results = cls.all()
        results = results.fetch(999, rpc= db.create_rpc(read_policy=db.EVENTUAL_CONSISTENCY ) )
        return results
#    @classmethod
#    def jsonAll(cls, inclCapital=False):
#        results=cls.getAll()
#        if not results is None:
#           results=[it.json(True) for it in results]
#        return results   
            
            
